package com.farm.service.impl;
import com.farm.dao.CropsMapper;
import com.farm.opjo.Crops;
import com.farm.reception.CropsDate;
import com.farm.opjo.CropsDisease;
import com.farm.opjo.CropsIndex;
import com.farm.service.CropsDiseaseService;
import com.farm.service.CropsIndexService;
import com.farm.service.CropsService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
@Service
public class CropsServiceImpl implements CropsService {
    @Autowired
    private CropsMapper cropsmapper;

    @Autowired
    private CropsDiseaseService cropsDiseaseService;

    @Autowired
    private CropsIndexService cropsIndexService;

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */

    @Override
    public Page<Crops> selectPage(int page, int size) {
        PageHelper.startPage ( page,size );
        return (Page<Crops>)cropsmapper.selectAll ();
    }

    /**
     * 新增
     * @param cropsDate
     * @return
     * 1.新增农作物
     * 2.新增农作物对应的病虫害中间表
     * 3.新增农作物对应的指标中间表
     */
    @Override
    public void add(CropsDate cropsDate) {
        try {

            cropsmapper.insert ( cropsDate.getCrops () );  //1.新增农作物

            List<CropsDisease> cropsDiseaseList = cropsDate.getCropsDiseaseList ();//2.新增农作物对应的病虫害中间表

            for (CropsDisease cropsDisease : cropsDiseaseList) {
                cropsDiseaseService.add ( cropsDisease );
            }

            List<CropsIndex> cropsIndexList = cropsDate.getCropsIndexList (); //3.新增农作物对应的指标中间表
            for (CropsIndex cropsIndex : cropsIndexList) {
                cropsIndexService.add (cropsIndex);
            }
        }catch (Exception e){
            System.out.println ("添加农作物失败");
        }
    }

    /**
     * 根据name查询对应的农作物
     * @param name
     * @return
     */
    @Override
    public Crops findByName(String name) {
        Crops crops = cropsmapper.findByName ( name );
        return crops;
    }

    /**
     * 更新
     * 1.先更新两张（指标，病虫害）中间表
     * 2.更新农作物
     * @param cropsDate
     * @return
     */
    @Override
    public void update(CropsDate cropsDate) {
        try {

            List<CropsIndex> cropsIndexList = cropsDate.getCropsIndexList ();//1.遍历 更新农作物指标中间表
            for (CropsIndex cropsIndex : cropsIndexList) {
                cropsIndexService.update ( cropsIndex );
            }

            List<CropsDisease> cropsDiseaseList = cropsDate.getCropsDiseaseList ();//2.遍历 更新农作物病虫害中间表
            for (CropsDisease cropsDisease : cropsDiseaseList) {
                cropsDiseaseService.update ( cropsDisease );
            }

            cropsmapper.updateByPrimaryKey ( cropsDate.getCrops ()); //3.更新农作物
        }catch (Exception e){
            System.out.println ("农作物更新失败");
        }

    }

    @Override
    public Crops findByCropsId(Integer id) {

        Crops crops = cropsmapper.selectByPrimaryKey(id);
        return crops;
    }

    /**
     *删除需要查询 指标（t_crops_index），病虫害(t_crops_disease),大田管理(t_cust_farmland_crops)
     *根据Id查询中间表没有数据才可以进行删除
     * @param id
     * @return
     */
    @Override
    public void delete(int id) {

        if (cropsIndexService.findByCId ( id )!=null&&cropsDiseaseService.findByCId ( id )!=null){//查询中间不存在对应的数据

            cropsmapper.deleteByPrimaryKey ( id );//删除农作物
        }
    }
}
